// 常购清单
var basePage = require('../../../common/BasePage');
var baseList = require('../../../common/BaseList');
Page({
  behaviors: [basePage, baseList],

  /**
   * 页面的初始数据
   */
  data: {
    reqUrl: "/home/often/index",
    total_price: 0,
    selectedAll: false,
	reqData: {},
  },
  
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  	let warehouse= getApp().globalData.warehouse;
	this.data.reqUrl = '/home/often/index?wh_id=' + warehouse.wh_id;
	this.req_items();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },
  /**格式化列表中的数据 */
  init_val(val, index, arr) {
    val.thumb = this.get_thumb(val.product_img);
  },
  often_change(e) {
    let index = e.currentTarget.dataset.index;
    let item = this.data.itemArr[index];
    item.selected = !item.selected;
    let total_price = 0;
    this.data.itemArr.map(often => {
      if (often.selected) {
        // total_price += often.product_price * +often.sell_num * 100;
		total_price += often.product_price * +often.sell_num ;
      }
    });
    this.setData({
      [`itemArr[${index}].selected`]: item.selected,
      total_price
    });

  },
  num_change(e) {
    // console.log(e);
    let index = e.currentTarget.dataset.index;
    let item = this.data.itemArr[index];
    item.sell_num = e.detail;

    let total_price = 0;
    this.data.itemArr.map(often => {
      if (often.selected) {
        // total_price += often.product_price * +often.sell_num * 100;
		total_price += often.product_price * +often.sell_num ;
      }
    });
    this.setData({
      [`itemArr[${index}].sell_num`]: item.sell_num,
      total_price
    });
  },

  delete_click(e) {
    let that = this;
    let index = e.currentTarget.dataset.index;
    let item = this.data.itemArr[index];
    wx.showModal({
      cancelText: '取消',
      confirmText: '确认删除',
      content: '是否删除' + item.product_name,
      showCancel: true,
      success: (result) => {
        if (result.confirm) {
          that.request({
            url: this.data.host + '/home/often/oftenDelete',
            data: {
              often_ids: item.often_id
            },
            loadingTip: '正在提交',
            method: "post"
          }, res => {
            that.data.itemArr.splice(index, 1);
            let total_price = 0;
            that.data.itemArr.map(often => {
              if (often.selected) {
                // total_price += often.product_price * +often.sell_num * 100;
				total_price += often.product_price * +often.sell_num ;
              }
            });
            that.setData({
              itemArr: that.data.itemArr,
              total_price
            })
          });
        }
      },
    })

  },
  selectedAll_click() {
    this.data.selectedAll = !this.data.selectedAll;
    let total_price = 0;

    this.data.itemArr.map(often => {
      often.selected = this.data.selectedAll;
      if (often.selected) {
        // total_price += often.product_price * +often.sell_num * 100;
		total_price += often.product_price * +often.sell_num ;
      }
    });
    this.setData({
      selectedAll: this.data.selectedAll,
      itemArr: this.data.itemArr,
      total_price
    })

  },
  onSubmit() {
    let arr = [];
    this.data.itemArr.map(often => {
      if (often.selected) {
        arr.push({
          often_id: often.often_id,
          nums: often.sell_num
        })
      }
    });
    if (!arr.length) {
      return wx.showToast({
        title: '请先选择商品',
        icon: 'none'
      });
    }

    this.request({
      url: this.data.host + '/home/often/handle',
      data: {
        goods: JSON.stringify(arr),
      },
      loadingTip: '正在运送至购物车...',
      method: "post"
    }, this.add_ok);
  },
  add_ok(res) {
    
    wx.showModal({
      cancelText: '继续添加',
      confirmText: '确定',
      content: '添加成功,是否前往购物车？',
      showCancel:true,
      success: (result) => {
        if (result.confirm){
          wx.switchTab({
            url: '/pages/tabbar/cart/cart',
          })
        }
      },
    })
  },
  toCat(){
	  wx.switchTab({
	  	url: "/pages/cat/Index",
	  })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

})